<template>
  <div class="hotel-detail">
    <!-- 返回按钮 -->
    <div class="back-button">
      <a-button @click="goBack">
        <template #icon><left-outlined /></template>
        返回主页
      </a-button>
    </div>

    <!-- 酒店主图 -->
    <div class="hotel-banner" :style="{ backgroundImage: `url(${hotelDetail.mainImage})` }">
      <div class="banner-content">
        <div class="hotel-title">
          <h1>{{ hotelDetail.name }}</h1>
          <div class="hotel-rating">
            <a-rate v-model:value="hotelDetail.rating" disabled allow-half />
            <span class="rating-score">{{ hotelDetail.rating }}分</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 酒店基本信息 -->
    <div class="hotel-content">
      <div class="quick-info">
        <div class="info-item">
          <environment-outlined />
          <span>{{ hotelDetail.address }}</span>
        </div>
        <div class="features">
          <div class="feature-item">
            <wifi-outlined />
            <span>免费WiFi</span>
          </div>
          <div class="feature-item">
            <car-outlined />
            <span>停车场</span>
          </div>
          <div class="feature-item">
            <experiment-outlined />
            <span>户外泳池</span>
          </div>
        </div>
        <a-button type="primary" size="large" class="book-now" @click="scrollToRooms">立即预订</a-button>
      </div>

      <div class="main-content">
        <!-- 左侧内容区 -->
        <div class="content-main">
          <!-- 导航菜单 -->
          <a-tabs v-model:activeKey="activeTab" class="hotel-tabs">
            <a-tab-pane key="overview" tab="概览">
              <hotel-overview :description="hotelDetail.description" />
            </a-tab-pane>
            <a-tab-pane key="rooms" tab="房间">
              <hotel-rooms 
                @room-select="handleRoomSelect"
                @search="handleSearch"
              />
            </a-tab-pane>
            <a-tab-pane key="amenities" tab="设施">
              <hotel-amenities />
            </a-tab-pane>
            <a-tab-pane key="reviews" tab="评论">
              <hotel-reviews :hotel-id="hotelId" />
            </a-tab-pane>
            <a-tab-pane key="location" tab="位置">
              <hotel-location :address="hotelDetail.address" :hotel-id="hotelId" />
            </a-tab-pane>
            <a-tab-pane key="policy" tab="政策">
              <div class="hotel-policy">
                <div class="section-header">
                  <file-protect-outlined class="section-icon" />
                  <h2>酒店政策</h2>
                </div>
                
                <div class="policy-container" v-if="hotelDetail.policy">
                  <div class="policy-card">
                    <h3>入住与退房</h3>
                    <div class="policy-item">
                      <clock-circle-outlined />
                      <span>入住时间: {{ hotelDetail.policy.checkInTime }}</span>
                    </div>
                    <div class="policy-item">
                      <clock-circle-outlined />
                      <span>退房时间: {{ hotelDetail.policy.checkOutTime }}</span>
                    </div>
                  </div>
                  
                  <div class="policy-card">
                    <h3>预订政策</h3>
                    <div class="policy-item">
                      <calendar-outlined />
                      <span>最早可提前{{ hotelDetail.policy.minAdvanceReservation || 0 }}天预订</span>
                    </div>
                    <div class="policy-item">
                      <calendar-outlined />
                      <span>最晚可提前{{ hotelDetail.policy.maxAdvanceReservation || 90 }}天预订</span>
                    </div>
                    <div class="policy-item">
                      <credit-card-outlined />
                      <span>支持的付款方式: 
                        <a-tag v-for="method in hotelDetail.policy.paymentMethods" :key="method" color="blue">
                          {{ getPaymentMethodName(method) }}
                        </a-tag>
                      </span>
                    </div>
                    <div class="policy-item" v-if="hotelDetail.policy.prepaymentRequired">
                      <dollar-outlined />
                      <span>需要预付款 ({{ hotelDetail.policy.prepaymentPercent }}%)</span>
                    </div>
                  </div>
                  
                  <div class="policy-card">
                    <h3>取消政策</h3>
                    <div class="policy-item">
                      <exclamation-circle-outlined />
                      <span>入住前{{ hotelDetail.policy.freeCancellationHours }}小时可免费取消</span>
                    </div>
                    <div class="policy-item">
                      <warning-outlined />
                      <span>逾期取消将收取{{ hotelDetail.policy.cancellationFeePercent }}%的费用</span>
                    </div>
                  </div>
                  
                  <div class="policy-card">
                    <h3>其他政策</h3>
                    <div class="policy-item">
                      <team-outlined />
                      <span>{{ hotelDetail.policy.childrenAllowed ? '允许' : '不允许' }}儿童入住 
                        <template v-if="hotelDetail.policy.childrenAllowed">
                          ({{ hotelDetail.policy.childAgeThreshold }}岁以下视为儿童)
                        </template>
                      </span>
                    </div>
                    <div class="policy-item">
                      <heart-outlined />
                      <span>{{ hotelDetail.policy.petsAllowed ? '允许' : '不允许' }}携带宠物
                        <template v-if="hotelDetail.policy.petsAllowed && hotelDetail.policy.petFee">
                          (额外收费: ¥{{ hotelDetail.policy.petFee }}/晚)
                        </template>
                      </span>
                    </div>
                    <div class="policy-item">
                      <border-outlined />
                      <span>{{ hotelDetail.policy.extraBedAllowed ? '允许' : '不允许' }}加床
                        <template v-if="hotelDetail.policy.extraBedAllowed && hotelDetail.policy.extraBedFee">
                          (额外收费: ¥{{ hotelDetail.policy.extraBedFee }}/晚)
                        </template>
                      </span>
                    </div>
                  </div>
                </div>
                
                <div class="policy-container" v-else>
                  <a-empty description="暂无政策信息" />
                </div>
              </div>
            </a-tab-pane>
          </a-tabs>
        </div>

        <!-- 右侧预订表单 -->
        <div class="content-side" v-if="selectedRoom">
          <booking-form
            :hotel-id="hotelId"
            :room-id="selectedRoom"
            :room-price="selectedRoomPrice"
            :max-guests="selectedRoomCapacity"
            @booking-success="handleBookingSuccess"
          />
        </div>
      </div>
    </div>

    <!-- <div class="floating-bar" :class="{ 'visible': showFloatingBar }">
      <div class="floating-content">
        <div class="floating-info">
          <h3>{{ hotelDetail.name }}</h3>
          <div class="floating-rating">
            <a-rate :value="hotelDetail.rating" disabled allow-half />
            <span>{{ hotelDetail.rating }}分</span>
          </div>
        </div>
        <a-button type="primary" size="large" @click="scrollToRooms">立即预订</a-button>
      </div>
    </div> -->
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { 
  WifiOutlined,
  CarOutlined,
  ExperimentOutlined,
  EnvironmentOutlined,
  LeftOutlined,
  FileProtectOutlined,
  ClockCircleOutlined,
  CalendarOutlined,
  CreditCardOutlined,
  DollarOutlined,
  ExclamationCircleOutlined,
  WarningOutlined,
  TeamOutlined,
  HeartOutlined,
  BorderOutlined
} from '@ant-design/icons-vue'
import HotelOverview from '@/components/hotel/HotelOverview.vue'
import HotelRooms from '@/components/hotel/HotelRooms.vue'
import HotelAmenities from '@/components/hotel/HotelAmenities.vue'
import HotelReviews from '@/components/hotel/HotelReviews.vue'
import HotelLocation from '@/components/hotel/HotelLocation.vue'
import BookingForm from '@/components/hotel/BookingForm.vue'
import { useHotelDetailStore } from '@/stores/hotelDetailStrore'
import { storeToRefs } from 'pinia'
import { message } from 'ant-design-vue'
import type { HotelRoom } from '@/stores/hotelDetailStrore'

const router = useRouter()
const route = useRoute()
const activeTab = ref('overview')
const showFloatingBar = ref(false)
const selectedRoom = ref<number | null>(null)
const selectedRoomPrice = ref(0)
const selectedRoomCapacity = ref(2)
const hotelId = ref<string | number>(route.params.id ? String(route.params.id) : '1')

// 使用酒店详情store
const hotelDetailStore = useHotelDetailStore()
const { hotelDetail, loading: storeLoading } = storeToRefs(hotelDetailStore)

// 支付方式名称映射
const getPaymentMethodName = (method: string) => {
  const methodMap: Record<string, string> = {
    'credit': '信用卡',
    'debit': '借记卡',
    'cash': '现金',
    'alipay': '支付宝',
    'wechat': '微信支付'
  }
  return methodMap[method] || method
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 滚动到房间区域
const scrollToRooms = () => {
  activeTab.value = 'rooms'
  setTimeout(() => {
    const roomsElement = document.querySelector('.hotel-tabs')
    if (roomsElement) {
      roomsElement.scrollIntoView({ behavior: 'smooth' })
    }
  }, 100)
}

// 处理房间选择
const handleRoomSelect = (room: HotelRoom) => {
  selectedRoom.value = room.id
  selectedRoomPrice.value = room.price
  selectedRoomCapacity.value = room.capacity
}

// 处理搜索
const handleSearch = (params: any) => {
  const { dateRange, guestCount } = params
  const checkIn = dateRange[0].format('YYYY-MM-DD')
  const checkOut = dateRange[1].format('YYYY-MM-DD')
  
  message.info(`搜索条件：入住日期 ${checkIn}，退房日期 ${checkOut}，入住人数 ${guestCount}人`)
  
  // 这里可以调用API进行搜索，或者更新当前页面的过滤条件
}

// 处理预订成功
const handleBookingSuccess = () => {
  activeTab.value = 'overview'
  selectedRoom.value = null
}

// 监听滚动显示/隐藏浮动预订栏
const handleScroll = () => {
  showFloatingBar.value = window.scrollY > 500
}

// 加载酒店详情数据
const loadHotelData = async () => {
  if (!hotelId.value) {
    message.error('酒店ID不能为空')
    return
  }
  
  try {
    // 直接使用store中的方法加载所有数据
    await hotelDetailStore.loadAllHotelData(hotelId.value)
  } catch (error) {
    console.error('加载酒店详情失败:', error)
    message.error('加载酒店详情失败')
  }
}

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
  
  // 重置store状态
  hotelDetailStore.resetState()
  
  // 加载所有酒店数据
  loadHotelData()
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
</script>

<style scoped>
.hotel-detail {
  min-height: 100vh;
  background: #f5f7fa;
  color: #333;
}

.hotel-banner {
  height: 70vh;
  min-height: 500px;
  background-size: cover;
  background-position: center;
  position: relative;
  background-attachment: fixed;
}

.hotel-banner::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.1), rgba(0,0,0,0.7));
}

.banner-content {
  position: relative;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px;
  display: flex;
  align-items: flex-end;
}

.hotel-title {
  color: white;
}

.hotel-title h1 {
  font-size: 42px;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 8px rgba(0,0,0,0.3);
  letter-spacing: -0.5px;
}

.hotel-rating {
  margin-top: 16px;
  display: flex;
  align-items: center;
}

.rating-score {
  margin-left: 12px;
  font-size: 18px;
  font-weight: 600;
}

.hotel-content {
  max-width: 1200px;
  margin: -80px auto 40px;
  padding: 0 20px;
  position: relative;
  z-index: 1;
}

.quick-info {
  background: white;
  padding: 24px 32px;
  border-radius: 12px;
  margin-bottom: 32px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.quick-info:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(0,0,0,0.12);
}

.info-item {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #333;
  font-size: 16px;
}

.info-item .anticon {
  font-size: 20px;
  color: #1890ff;
}

.features {
  display: flex;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #555;
  font-size: 15px;
}

.feature-item .anticon {
  font-size: 18px;
  color: #1890ff;
}

.book-now {
  height: 44px;
  padding: 0 24px;
  font-size: 16px;
  border-radius: 8px;
  font-weight: 500;
  background: #1890ff;
  transition: all 0.3s ease;
}

.book-now:hover {
  background: #0c7ad9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.main-content {
  display: grid;
  grid-template-columns: 1fr 350px;
  gap: 24px;
}

.content-main {
  width: 100%;
}

.content-side {
  width: 100%;
  position: sticky;
  top: 24px;
  align-self: start;
}

.hotel-tabs {
  background: white;
  padding: 24px 32px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

.floating-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 16px;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.1);
  transform: translateY(100%);
  transition: transform 0.3s ease;
  z-index: 100;
}

.floating-bar.visible {
  transform: translateY(0);
}

.floating-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.floating-info {
  display: flex;
  flex-direction: column;
}

.floating-info h3 {
  margin: 0 0 4px;
  font-size: 18px;
  font-weight: 600;
}

.floating-rating {
  display: flex;
  align-items: center;
  gap: 8px;
}

.floating-rating span {
  color: #666;
}

:deep(.ant-tabs-nav) {
  margin-bottom: 24px;
}

:deep(.ant-tabs-tab) {
  padding: 12px 20px;
  font-size: 16px;
}

:deep(.ant-tabs-tab.ant-tabs-tab-active .ant-tabs-tab-btn) {
  font-weight: 600;
  color: #1890ff;
}

:deep(.ant-tabs-ink-bar) {
  background: #1890ff;
  height: 3px;
  border-radius: 3px;
}

:deep(.ant-rate-star) {
  color: #fadb14;
}

.back-button {
  position: absolute;
  top: 24px;
  left: 24px;
  z-index: 10;
}

.back-button button {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: all 0.3s ease;
}

.back-button button:hover {
  background: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

@media (max-width: 992px) {
  .main-content {
    grid-template-columns: 1fr;
  }
  
  .content-side {
    position: static;
  }
  
  .quick-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .features {
    width: 100%;
    justify-content: space-between;
  }
  
  .book-now {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .hotel-banner {
    height: 50vh;
    min-height: 400px;
  }
  
  .hotel-title h1 {
    font-size: 32px;
  }
  
  .features {
    flex-wrap: wrap;
    gap: 16px;
  }
  
  .floating-content {
    flex-direction: column;
    gap: 16px;
  }
  
  .floating-info {
    align-items: center;
  }
}

/* 添加政策相关样式 */
.hotel-policy {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.policy-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.policy-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.06);
  transition: all 0.3s ease;
}

.policy-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.1);
  transform: translateY(-2px);
}

.policy-card h3 {
  margin: 0 0 16px 0;
  font-size: 18px;
  color: #262626;
  font-weight: 500;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 12px;
}

.policy-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 16px;
  color: #595959;
}

.policy-item .anticon {
  color: #1890ff;
  margin-top: 3px;
}

.policy-item:last-child {
  margin-bottom: 0;
}

@media (max-width: 768px) {
  .policy-container {
    grid-template-columns: 1fr;
  }
}
</style> 